package com.xinyun.server.thread;

import com.xinyun.server.ChannelManager;
import com.xinyun.server.Session;
import com.xinyun.server.protocol.send.ScanAll;
import com.xinyun.server.protocol.send.ScanOne;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Map;

/**
 * Created by bubusy on 16/5/22.
 */
public class ScanAllThread extends Thread {

    private Log log = LogFactory.getLog(ScanAllThread.class);

    private ChannelFuture channelFuture;


    public ScanAllThread(ChannelFuture channelFuture) {

        this.channelFuture = channelFuture;
    }

    @Override
    public void run() {

        try {
            while (true) {
                Thread.sleep(1000 * 60 * 5);
                log.debug("扫描所有设备");
                Map<String, Session> channels = ChannelManager.getChannels();
                for (Session s : channels.values()) {
                    Channel channel = s.channel;
                    channel.writeAndFlush(new ScanAll());
                    log.debug("扫描单个设备");
                    //channel.writeAndFlush(new ScanOne(998, 2));
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
